package com.fankai.lease.web.app.controller.leasaterm;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fankai.lease.common.result.Result;
import com.fankai.lease.model.entity.LeaseTerm;
import com.fankai.lease.model.entity.RoomLeaseTerm;
import com.fankai.lease.web.app.service.LeaseAgreementService;
import com.fankai.lease.web.app.service.LeaseTermService;
import com.fankai.lease.web.app.service.RoomLeaseTermService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/app/term/")
@Tag(name = "租期信息")
public class LeaseTermController {
    @Autowired
    private LeaseTermService leaseTermService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @GetMapping("listByRoomId")
    @Operation(summary = "根据房间id获取可选获取租期列表")
    public Result<List<LeaseTerm>> list(@RequestParam Long id) {
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);

        List<RoomLeaseTerm> roomLeaseTermList = roomLeaseTermService.list(roomLeaseTermLambdaQueryWrapper);

        List<Long> leaseTermIds = roomLeaseTermList.stream().map(RoomLeaseTerm::getLeaseTermId).toList();
        List<LeaseTerm> leaseTermList = null;
        if (!leaseTermIds.isEmpty()) {
            LambdaQueryWrapper<LeaseTerm> leaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            leaseTermLambdaQueryWrapper.in(LeaseTerm::getId, leaseTermIds);
            leaseTermList = leaseTermService.list(leaseTermLambdaQueryWrapper);
        }
        return Result.ok(leaseTermList);
    }
}
